<!DOCTYPE html>
<html lang="zh-cmn-Hans">

<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge, chrome=1">
    <title>debounce</title>
    <style>
        #container{
            width: 100%; height: 200px; line-height: 200px; text-align: center; color: #fff; background-color: #444; font-size: 30px;
        }
    </style>
</head>

<body>
    <div id="container"></div>
    <script>
        /**
          * 防抖:执行最后一个入栈的计时器，之前入栈的计时器全部停止
        **/
        let container = document.getElementById("container")
        ,count = 1
        ,debounce = function(handle,wait,nowExecute){
            let timer ;
            return function(...args){
                //清空上一次定时器
                timer &&　clearTimeout(timer)
                timer = setTimeout(()=>{
                    handle.apply(this,args);
                },wait);
                if(nowExecute){
                    nowExecute = false;
                    handle.apply(this,args);
                }
            }
        }
        container.addEventListener('mousemove',debounce(function(){
            container.innerHTML = count++;
        },300,true))
    </script>
</body>

</html>